﻿; (function ($)
{
        $.fn.jcTitle = function (options)
        {
                //默认样式，可以在这里修改
                var defaults = {
                        background: '#f7eebf',
                        color: '#393939',
                        opacity: '0.8',
                        border: 'solid 1px #968212',
                        fontSize: 12,
                        position: 'absolute',
                        zIndex: 9999,
                        textShadow: 'none',
                        borderRadius: 3,
                        padding: 6,
                        follow: true
                },
                    options = $.extend(defaults, options);

                //始终返回jquery对象以允许链式调用
                return this.each(function ()
                {
                        var elem = $(this),title = options.content || elem.attr('title'),_ww, _wh, _tw, _th;  //浏览器窗口 宽、高 ，创建的title层的宽、高

                        if (title != '')
                        {
                                //创建title层
                                var junchengTitle = $('<div />');

                                //删除title属性，阻止浏览器默认的title提示
                                elem.attr('title', '').hover(function (e)
                                {
                                        _ww = $(window).width(), _wh = $(window).height();
                                        junchengTitle
                                            .html(title)
                                            .appendTo('body')
                                            .hide()
                                            .css(options)
                                        .fadeIn(500);
                                        _tw = $('body div').last().outerWidth(), _th = $('body div').last().outerHeight();
                                        if (options.follow)
                                        {
                                                elem.mousemove(function (e)
                                                {
                                                        var _top = e.pageY + 8, _left = e.pageX + 8;

                                                        if (_top > _wh + $(document).scrollTop() - _th)
                                                        { _top = e.pageY - _th - 8; }

                                                        if (_left > _ww + $(document).scrollLeft() - _tw)
                                                        { _left = e.pageX - 8 - _tw; }

                                                        junchengTitle.css({
                                                                top: _top,
                                                                left: _left
                                                        });
                                                });
                                        }
                                        else
                                        {
                                                var _top = e.pageY + 8, _left = e.pageX + 8;

                                                if (_top > _wh + $(document).scrollTop() - _th)
                                                { _top = e.pageY - _th - 8; }

                                                if (_left > _ww + $(document).scrollLeft() - _tw)
                                                { _left = e.pageX - 8-_tw;}

                                                junchengTitle.css({
                                                        top: _top,
                                                        left: _left
                                                });
                                        }
                                        junchengTitle.fadeIn(500);
                                }, function ()
                                {
                                        junchengTitle.remove(); //删除层元素
                                });
                        }                      
                });
        }
})(jQuery);